{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Python Language Basics, IPython, and Jupyter Notebooks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.random.seed(12345)\n",
    "np.set_printoptions(precision=4, suppress=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## The Python Interpreter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "$ python\n",
    "Python 3.6.0 | packaged by conda-forge | (default, Jan 13 2017, 23:17:12)\n",
    "[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>> a = 5\n",
    ">>> print(a)\n",
    "5\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "print('Hello world')\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "$ python hello_world.py\n",
    "Hello world\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```shell\n",
    "$ ipython\n",
    "Python 3.6.0 | packaged by conda-forge | (default, Jan 13 2017, 23:17:12)\n",
    "Type \"copyright\", \"credits\" or \"license\" for more information.\n",
    "\n",
    "IPython 5.1.0 -- An enhanced Interactive Python.\n",
    "?         -> Introduction and overview of IPython's features.\n",
    "%quickref -> Quick reference.\n",
    "help      -> Python's own help system.\n",
    "object?   -> Details about 'object', use 'object??' for extra details.\n",
    "\n",
    "In [1]: %run hello_world.py\n",
    "Hello world\n",
    "\n",
    "In [2]:\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## IPython Basics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Running the IPython Shell"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "$ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: -0.20470765948471295,\n",
       " 1: 0.47894333805754824,\n",
       " 2: -0.5194387150567381,\n",
       " 3: -0.55573030434749,\n",
       " 4: 1.9657805725027142,\n",
       " 5: 1.3934058329729904,\n",
       " 6: 0.09290787674371767}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "data = {i : np.random.randn() for i in range(7)}\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    ">>> from numpy.random import randn\n",
    ">>> data = {i : randn() for i in range(7)}\n",
    ">>> print(data)\n",
    "{0: -1.5948255432744511, 1: 0.10569006472787983, 2: 1.972367135977295,\n",
    "3: 0.15455217573074576, 4: -0.24058577449429575, 5: -1.2904897053651216,\n",
    "6: 0.3308507317325902}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Running the Jupyter Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```shell\n",
    "$ jupyter notebook\n",
    "[I 15:20:52.739 NotebookApp] Serving notebooks from local directory:\n",
    "/home/wesm/code/pydata-book\n",
    "[I 15:20:52.739 NotebookApp] 0 active kernels\n",
    "[I 15:20:52.739 NotebookApp] The Jupyter Notebook is running at:\n",
    "http://localhost:8888/\n",
    "[I 15:20:52.740 NotebookApp] Use Control-C to stop this server and shut down\n",
    "all kernels (twice to skip confirmation).\n",
    "Created new window in existing browser session.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Tab Completion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```\n",
    "In [1]: an_apple = 27\n",
    "\n",
    "In [2]: an_example = 42\n",
    "\n",
    "In [3]: an\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```\n",
    "In [3]: b = [1, 2, 3]\n",
    "\n",
    "In [4]: b.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```\n",
    "In [1]: import datetime\n",
    "\n",
    "In [2]: datetime.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```\n",
    "In [7]: datasets/movielens/\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Introspection"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```\n",
    "In [8]: b = [1, 2, 3]\n",
    "\n",
    "In [9]: b?\n",
    "Type:       list\n",
    "String Form:[1, 2, 3]\n",
    "Length:     3\n",
    "Docstring:\n",
    "list() -> new empty list\n",
    "list(iterable) -> new list initialized from iterable's items\n",
    "\n",
    "In [10]: print?\n",
    "Docstring:\n",
    "print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
    "\n",
    "Prints the values to a stream, or to sys.stdout by default.\n",
    "Optional keyword arguments:\n",
    "file:  a file-like object (stream); defaults to the current sys.stdout.\n",
    "sep:   string inserted between values, default a space.\n",
    "end:   string appended after the last value, default a newline.\n",
    "flush: whether to forcibly flush the stream.\n",
    "Type:      builtin_function_or_method\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "def add_numbers(a, b):\n",
    "    \"\"\"\n",
    "    Add two numbers together\n",
    "\n",
    "    Returns\n",
    "    -------\n",
    "    the_sum : type of arguments\n",
    "    \"\"\"\n",
    "    return a + b\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [11]: add_numbers?\n",
    "Signature: add_numbers(a, b)\n",
    "Docstring:\n",
    "Add two numbers together\n",
    "\n",
    "Returns\n",
    "-------\n",
    "the_sum : type of arguments\n",
    "File:      <ipython-input-9-6a548a216e27>\n",
    "Type:      function\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [12]: add_numbers??\n",
    "Signature: add_numbers(a, b)\n",
    "Source:\n",
    "def add_numbers(a, b):\n",
    "    \"\"\"\n",
    "    Add two numbers together\n",
    "\n",
    "    Returns\n",
    "    -------\n",
    "    the_sum : type of arguments\n",
    "    \"\"\"\n",
    "    return a + b\n",
    "File:      <ipython-input-9-6a548a216e27>\n",
    "Type:      function\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [13]: np.*load*?\n",
    "np.__loader__\n",
    "np.load\n",
    "np.loads\n",
    "np.loadtxt\n",
    "np.pkgload\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### The %run Command"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "def f(x, y, z):\n",
    "    return (x + y) / z\n",
    "\n",
    "a = 5\n",
    "b = 6\n",
    "c = 7.5\n",
    "\n",
    "result = f(a, b, c)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [14]: %run ipython_script_test.py\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [15]: c\n",
    "Out [15]: 7.5\n",
    "\n",
    "In [16]: result\n",
    "Out[16]: 1.4666666666666666\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    ">>> %load ipython_script_test.py\n",
    "\n",
    "    def f(x, y, z):\n",
    "        return (x + y) / z\n",
    "\n",
    "    a = 5\n",
    "    b = 6\n",
    "    c = 7.5\n",
    "\n",
    "    result = f(a, b, c)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Interrupting running code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Executing Code from the Clipboard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "x = 5\n",
    "y = 7\n",
    "if x > 5:\n",
    "    x += 1\n",
    "\n",
    "    y = 8\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [17]: %paste\n",
    "x = 5\n",
    "y = 7\n",
    "if x > 5:\n",
    "    x += 1\n",
    "\n",
    "    y = 8\n",
    "## -- End pasted text --\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [18]: %cpaste\n",
    "Pasting code; enter '--' alone on the line to stop or use Ctrl-D.\n",
    ":x = 5\n",
    ":y = 7\n",
    ":if x > 5:\n",
    ":    x += 1\n",
    ":\n",
    ":    y = 8\n",
    ":--\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Terminal Keyboard Shortcuts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### About Magic Commands"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [20]: a = np.random.randn(100, 100)\n",
    "\n",
    "In [20]: %timeit np.dot(a, a)\n",
    "10000 loops, best of 3: 20.9 µs per loop\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [21]: %debug?\n",
    "Docstring:\n",
    "::\n",
    "\n",
    "  %debug [--breakpoint FILE:LINE] [statement [statement ...]]\n",
    "\n",
    "Activate the interactive debugger.\n",
    "\n",
    "This magic command support two ways of activating debugger.\n",
    "One is to activate debugger before executing code.  This way, you\n",
    "can set a break point, to step through the code from the point.\n",
    "You can use this mode by giving statements to execute and optionally\n",
    "a breakpoint.\n",
    "\n",
    "The other one is to activate debugger in post-mortem mode.  You can\n",
    "activate this mode simply running %debug without any argument.\n",
    "If an exception has just occurred, this lets you inspect its stack\n",
    "frames interactively.  Note that this will always work only on the last\n",
    "traceback that occurred, so you must call this quickly after an\n",
    "exception that you wish to inspect has fired, because if another one\n",
    "occurs, it clobbers the previous one.\n",
    "\n",
    "If you want IPython to automatically do this on every exception, see\n",
    "the %pdb magic for more details.\n",
    "\n",
    "positional arguments:\n",
    "  statement             Code to run in debugger. You can omit this in cell\n",
    "                        magic mode.\n",
    "\n",
    "optional arguments:\n",
    "  --breakpoint <FILE:LINE>, -b <FILE:LINE>\n",
    "                        Set break point at LINE in FILE.\n",
    "\n",
    "```                        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [22]: %pwd\n",
    "Out[22]: '/home/wesm/code/pydata-book\n",
    "\n",
    "In [23]: foo = %pwd\n",
    "\n",
    "In [24]: foo\n",
    "Out[24]: '/home/wesm/code/pydata-book'\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Matplotlib Integration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [26]: %matplotlib\n",
    "Using matplotlib backend: Qt4Agg\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [26]: %matplotlib inline\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Python Language Basics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Language Semantics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Indentation, not braces"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "for x in array:\n",
    "    if x < pivot:\n",
    "        less.append(x)\n",
    "    else:\n",
    "        greater.append(x)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "a = 5; b = 6; c = 7\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Everything is an object"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Comments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "results = []\n",
    "for line in file_handle:\n",
    "    # keep the empty lines for now\n",
    "    # if len(line) == 0:\n",
    "    #   continue\n",
    "    results.append(line.replace('foo', 'bar'))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "print(\"Reached this line\")  # Simple status report\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Function and object method calls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```\n",
    "result = f(x, y, z)\n",
    "g()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```\n",
    "obj.some_method(x, y, z)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "result = f(a, b, c, d=5, e='foo')\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Variables and argument passing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = [1, 2, 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "b = a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a.append(4)\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "def append_element(some_list, element):\n",
    "    some_list.append(element)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [27]: data = [1, 2, 3]\n",
    "\n",
    "In [28]: append_element(data, 4)\n",
    "\n",
    "In [29]: data\n",
    "Out[29]: [1, 2, 3, 4]\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Dynamic references, strong types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = 5\n",
    "type(a)\n",
    "a = 'foo'\n",
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "'5' + 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = 4.5\n",
    "b = 2\n",
    "# String formatting, to be visited later\n",
    "print('a is {0}, b is {1}'.format(type(a), type(b)))\n",
    "a / b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = 5\n",
    "isinstance(a, int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = 5; b = 4.5\n",
    "isinstance(a, (int, float))\n",
    "isinstance(b, (int, float))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Attributes and methods"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "In [1]: a = 'foo'\n",
    "\n",
    "In [2]: a.<Press Tab>\n",
    "a.capitalize  a.format      a.isupper     a.rindex      a.strip\n",
    "a.center      a.index       a.join        a.rjust       a.swapcase\n",
    "a.count       a.isalnum     a.ljust       a.rpartition  a.title\n",
    "a.decode      a.isalpha     a.lower       a.rsplit      a.translate\n",
    "a.encode      a.isdigit     a.lstrip      a.rstrip      a.upper\n",
    "a.endswith    a.islower     a.partition   a.split       a.zfill\n",
    "a.expandtabs  a.isspace     a.replace     a.splitlines\n",
    "a.find        a.istitle     a.rfind       a.startswith\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = 'foo'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "getattr(a, 'split')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Duck typing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "def isiterable(obj):\n",
    "    try:\n",
    "        iter(obj)\n",
    "        return True\n",
    "    except TypeError: # not iterable\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "isiterable('a string')\n",
    "isiterable([1, 2, 3])\n",
    "isiterable(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "if not isinstance(x, list) and isiterable(x):\n",
    "    x = list(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Imports"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "```python\n",
    "# some_module.py\n",
    "PI = 3.14159\n",
    "\n",
    "def f(x):\n",
    "    return x + 2\n",
    "\n",
    "def g(a, b):\n",
    "    return a + b\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "import some_module\n",
    "result = some_module.f(5)\n",
    "pi = some_module.PI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "from some_module import f, g, PI\n",
    "result = g(5, PI)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "import some_module as sm\n",
    "from some_module import PI as pi, g as gf\n",
    "\n",
    "r1 = sm.f(pi)\n",
    "r2 = gf(6, pi)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Binary operators and comparisons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "5 - 7\n",
    "12 + 21.5\n",
    "5 <= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = [1, 2, 3]\n",
    "b = a\n",
    "c = list(a)\n",
    "a is b\n",
    "a is not c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a == c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = None\n",
    "a is None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Mutable and immutable objects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a_list = ['foo', 2, [4, 5]]\n",
    "a_list[2] = (3, 4)\n",
    "a_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a_tuple = (3, 5, (4, 5))\n",
    "a_tuple[1] = 'four'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Scalar Types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Numeric types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "ival = 17239871\n",
    "ival ** 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "fval = 7.243\n",
    "fval2 = 6.78e-5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "3 / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "3 // 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Strings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "a = 'one way of writing a string'\n",
    "b = \"another way\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "c = \"\"\"\n",
    "This is a longer string that\n",
    "spans multiple lines\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "c.count('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = 'this is a string'\n",
    "a[10] = 'f'\n",
    "b = a.replace('string', 'longer string')\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = 5.6\n",
    "s = str(a)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "s = 'python'\n",
    "list(s)\n",
    "s[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "s = '12\\\\34'\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "s = r'this\\has\\no\\special\\characters'\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = 'this is the first half '\n",
    "b = 'and this is the second half'\n",
    "a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "template = '{0:.2f} {1:s} are worth US${2:d}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "template.format(4.5560, 'Argentine Pesos', 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Bytes and Unicode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "val = \"español\"\n",
    "val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "val_utf8 = val.encode('utf-8')\n",
    "val_utf8\n",
    "type(val_utf8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "val_utf8.decode('utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "val.encode('latin1')\n",
    "val.encode('utf-16')\n",
    "val.encode('utf-16le')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "bytes_val = b'this is bytes'\n",
    "bytes_val\n",
    "decoded = bytes_val.decode('utf8')\n",
    "decoded  # this is str (Unicode) now"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Booleans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "True and True\n",
    "False or True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Type casting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "s = '3.14159'\n",
    "fval = float(s)\n",
    "type(fval)\n",
    "int(fval)\n",
    "bool(fval)\n",
    "bool(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = None\n",
    "a is None\n",
    "b = 5\n",
    "b is not None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "def add_and_maybe_multiply(a, b, c=None):\n",
    "    result = a + b\n",
    "\n",
    "    if c is not None:\n",
    "        result = result * c\n",
    "\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "type(None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Dates and times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from datetime import datetime, date, time\n",
    "dt = datetime(2011, 10, 29, 20, 30, 21)\n",
    "dt.day\n",
    "dt.minute"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "dt.date()\n",
    "dt.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "dt.strftime('%m/%d/%Y %H:%M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "datetime.strptime('20091031', '%Y%m%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "dt.replace(minute=0, second=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "dt2 = datetime(2011, 11, 15, 22, 30)\n",
    "delta = dt2 - dt\n",
    "delta\n",
    "type(delta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "dt\n",
    "dt + delta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Control Flow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### if, elif, and else"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "if x < 0:\n",
    "    print('It's negative')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "if x < 0:\n",
    "    print('It's negative')\n",
    "elif x == 0:\n",
    "    print('Equal to zero')\n",
    "elif 0 < x < 5:\n",
    "    print('Positive but smaller than 5')\n",
    "else:\n",
    "    print('Positive and larger than or equal to 5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "a = 5; b = 7\n",
    "c = 8; d = 4\n",
    "if a < b or c > d:\n",
    "    print('Made it')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "4 > 3 > 2 > 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### for loops"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "for value in collection:\n",
    "    # do something with value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "sequence = [1, 2, None, 4, None, 5]\n",
    "total = 0\n",
    "for value in sequence:\n",
    "    if value is None:\n",
    "        continue\n",
    "    total += value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "sequence = [1, 2, 0, 4, 6, 5, 2, 1]\n",
    "total_until_5 = 0\n",
    "for value in sequence:\n",
    "    if value == 5:\n",
    "        break\n",
    "    total_until_5 += value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "for i in range(4):\n",
    "    for j in range(4):\n",
    "        if j > i:\n",
    "            break\n",
    "        print((i, j))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "for a, b, c in iterator:\n",
    "    # do something"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### while loops"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "x = 256\n",
    "total = 0\n",
    "while x > 0:\n",
    "    if total > 500:\n",
    "        break\n",
    "    total += x\n",
    "    x = x // 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "if x < 0:\n",
    "    print('negative!')\n",
    "elif x == 0:\n",
    "    # TODO: put something smart here\n",
    "    pass\n",
    "else:\n",
    "    print('positive!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "range(10)\n",
    "list(range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "list(range(0, 20, 2))\n",
    "list(range(5, 0, -1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "seq = [1, 2, 3, 4]\n",
    "for i in range(len(seq)):\n",
    "    val = seq[i]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "sum = 0\n",
    "for i in range(100000):\n",
    "    # % is the modulo operator\n",
    "    if i % 3 == 0 or i % 5 == 0:\n",
    "        sum += i"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Ternary expressions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "value = "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "if "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "x = 5\n",
    "'Non-negative' if x >= 0 else 'Negative'"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
